package com.xr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.dao.BookDao;
import com.xr.pojo.Goods;
import com.xr.service.GoodsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private BookDao bookDao;


    @Override
    public Boolean save(Goods goods) {
        return bookDao.insert(goods) > 0;
    }

    @Override
    public Boolean update(Goods goods) {
        return bookDao.updateById(goods) > 0;
    }

    @Override
    public Boolean delete(Integer id) {
        return bookDao.deleteById(id) > 0;
    }

    @Override
    public Goods getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    public List<Goods> getAll() {
        return bookDao.selectList(null);
    }

    @Override
    public IPage<Goods> getPage(int currentPage, int pageSize,Goods goods) {
        IPage page = new Page(currentPage,pageSize);
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<Goods>();
        lqw.like(Strings.isNotEmpty(goods.getName()),Goods::getName,goods.getName());
        lqw.like(Strings.isNotEmpty(goods.getInfo()),Goods::getInfo,goods.getInfo());
        return bookDao.selectPage(page,lqw);
    }
}
